const uefileService = require('../services/uefileService')

const download = async ctx => {
  const fileName = ctx.params.fileName
  try {
    uefileService.download(ctx, fileName)
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const addsyncFile = async ctx => {
  const data = ctx.request.body
  try {
    const instance = await uefileService.addsyncFile(data)
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '添加同步文件数据成功！',
      data: instance
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const updatesyncFile = async ctx => {
  const data = ctx.request.body
  try {
    await uefileService.updatesyncFile(data)
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '更新同步文件数据成功！'
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const getsyncFile = async ctx => {
  const { pageSize, pageNo, clusterId } = ctx.request.body
  try {
    const { records, total } = await uefileService.getsyncFile(
      pageSize,
      pageNo,
      clusterId
    )
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '获取ue工程文件列表成功！',
      data: records,
      page: {
        total: total,
        pageNo: pageNo,
        pages: Math.ceil(total / pageSize)
      }
    }
  } catch (error) {
    throw new HttpError(error.message, 400)
  }
}

const clustersyncFile = async ctx => {
  try {
    await uefileService.clustersyncFile()
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '已通知所有集群节点同步数据，请稍后！'
    }
  } catch (error) {
    throw new HttpError(error.message, 400)
  }
}
const deletesyncFile = async ctx => {
  const { ids } = ctx.request.body
  try {
    await uefileService.deletesyncFile(ids)
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '删除uefile数据成功！'
    }
  } catch (error) {
    throw new HttpError(error.message, 400)
  }
}
module.exports = {
  download,
  addsyncFile,
  updatesyncFile,
  getsyncFile,
  clustersyncFile,
  deletesyncFile
}
